package com.e.takeaway.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.e.takeaway.R;
import com.e.takeaway.po.MerChant;
import com.squareup.picasso.Picasso;

import java.util.List;

public class SearchAdapter extends BaseAdapter {
    private List<MerChant> merChants;
    private Context ctr;
    private LayoutInflater inflater;

    public SearchAdapter(Context context,List<MerChant> list){
        ctr = context;
        this.merChants = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return merChants.size();
    }

    @Override
    public Object getItem(int i) {
        return  merChants.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        MerChant merChant = merChants.get(i);
        MerChantAdapter.ViewHolder viewHolder = null;
        if (view == null){
            view = inflater.inflate(R.layout.item_list_merchant,viewGroup,false);
            viewHolder = new MerChantAdapter.ViewHolder();
            viewHolder.mer_name = (TextView) view.findViewById(R.id.mer_tv_name);
            viewHolder.mer_discount = (TextView) view.findViewById(R.id.mer_tv_discount);
            viewHolder.mer_sale = (TextView) view.findViewById(R.id.mer_tv_sale);
            viewHolder.mer_score = (TextView) view.findViewById(R.id.mer_tv_score);
            viewHolder.mer_start = (TextView) view.findViewById(R.id.mer_tv_start);
            viewHolder.mer_take = (TextView) view.findViewById(R.id.mer_tv_take);
            viewHolder.mer_time = (TextView) view.findViewById(R.id.mer_tv_time);
            viewHolder.mer_img = (ImageView) view.findViewById(R.id.mer_img);
            view.setTag(viewHolder);

        }else{
            viewHolder = (MerChantAdapter.ViewHolder) view.getTag();
        }
        viewHolder.mer_name.setText(merChant.getMerName());
        viewHolder.mer_time.setText(merChant.getMerTime()+"分钟");
        viewHolder.mer_take.setText("配送约￥"+merChant.getMerTake());
        viewHolder.mer_start.setText("约￥"+merChant.getMerStart());
        viewHolder.mer_score.setText(merChant.getMerScore()+"分");
        viewHolder.mer_discount.setText(merChant.getMerDiscount());
        viewHolder.mer_sale.setText("月售"+merChant.getMerSale());
        Picasso.with(ctr)
                .load(merChant.getMerImg())
                .placeholder(viewHolder.mer_img.getDrawable())
                .error(R.drawable.lunbo1)
                .into(viewHolder.mer_img);

        return view;

    }


    static class ViewHolder{
        public TextView mer_name;
        public TextView mer_score;
        public TextView mer_sale;
        public TextView mer_start;
        public TextView mer_take;
        public TextView mer_time;
        public TextView mer_discount;
        public ImageView mer_img;
    }

}
